package cn.wh.basic.service.impl;

import cn.wh.basic.mapper.BaseMapper;
import cn.wh.basic.query.BaseQuery;
import cn.wh.basic.service.IBaseService;
import cn.wh.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> mapper;

    @Override
    @Transactional
    public void save(T t) {
        mapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    @Transactional
    public void removeById(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public List<T> queryAll() {
        return mapper.loadAll();
    }

    @Override
    public T queryById(Long id) {
        return mapper.loadById(id);
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        Long total = mapper.queryCount(query);
        List<T> rows = mapper.queryData(query);

        return new PageList<T>(total,rows);
    }

    @Override
    public void deleteAll(List<Long> ids) {
        mapper.deleteAll(ids);
    }
}
